// pages/noteEdit/noteEdit.js
import request from '../../utils/request'
import { $toast } from '../../utils/util.js'
wx.cloud.init()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    noteContent: '',
    noteTitle: '',
    fileList: [],
    noteImg: '',
    show: false,
    actions: [{
      name: '美食',
    },
    {
      name: '旅行',
    },
    {
      name: '汽车',
    },
    {
      name: '时尚',
    },
    {
      name: '科技',
    },
    ],
    noteType: '',
    userId: '',
    nickname: '',
    actioins: '',
    selectNoteType: '选择分类'
  },

  noteContent(e) {
    // console.log(e.detail.html);
    this.setData({
      noteContent: e.detail.html
    })
  },
  noteTitle(e) {
    // console.log(e.detail);
    this.setData({
      noteTitle: e.detail
    })
  },

  noteImg(e) {
    // console.log(e);
    const { file } = e.detail
    wx.cloud.uploadFile({
      cloudPath: `${new Date().getTime()}.png`,//文件名
      filePath: file.url,   //图片的url

    }).then(res => {
      // console.log(res);
      let fileList = this.data.fileList
      fileList.push({ url: res.fileID })
      this.setData({
        noteImg: res.fileID,
        fileList: fileList
      })
    })
  },

  showAction() {
    this.setData({
      show: true
    })
  },

  onClose() {
    this.setData({
      show: false
    });
  },

  onSelect(e) {
    // console.log(event.detail);
    this.setData({
      noteType: e.detail.name,
      actioins: e.detail.name,
      selectNoteType: e.detail.name
    })
  },

  publish() {
    let { id, nickname } = wx.getStorageSync('userInfo')
    console.log(this.data.noteContent, this.data.noteTitle, this.data.noteImg, this.data.noteType, id, nickname);

    request('POST', '/note/publishNote', {
      noteContent: this.data.noteContent,
      noteTitle: this.data.noteTitle,
      noteImg: this.data.noteImg,
      noteType: this.data.noteType,
      useId: id,
      nickname: nickname
    }).then(res => {
      // console.log(res);
      $toast(res.data.mess, 'success')
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/noteClass/noteClass',
        })
      }, 1000)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})